#!/bin/bash

# Restrict command to sudo users
if [ "$EUID" -ne 0 ] ; then
  echo "Root priveleges are needed for this operation"
  exit 1
fi

if [ -f /etc/sonic/chassisdb.conf ]; then
  rexec all -c "sudo TSA chassis"
  echo "Please execute \"rexec all -c 'sudo config save -y'\" to preserve System mode in Maintenance after reboot\
  or config reload on all linecards"
  exit 0
fi
# toggle the mux to standby if dualtor and any mux active
if
[[ "$(sonic-cfggen -d -v DEVICE_METADATA.localhost.subtype | tr [:upper:] [:lower:])" == *"dualtor"* ]] &&
[[ $(show mux status | grep active | wc -l) > 0 ]];
then
  logger -t TSA -p user.info "Toggle all mux mode to standby"
  sudo config mux mode standby all
fi

/usr/bin/TS TSA
if [[ "$(sonic-cfggen -d -v DEVICE_METADATA.localhost.type)" == *"SpineRouter"* ]] ; then
  if [[ "$1" != "chassis" ]] ; then
    echo "Please execute 'sudo config save' to preserve System mode in Maintenance after reboot or config reload"
    echo -e "\nWARNING: Please execute 'TSA' on all other linecards of the chassis to fully isolate this device"    
  fi
else
  echo "Please execute 'sudo config save' to preserve System mode in Maintenance after reboot or config reload"
fi